/*
 * 项目名称:    wms-server-jeecg
 * 创建人员:    FanZhiwen
 * 创建时间:    2023-11-20
 */
package org.jeecg.modules.flow.callback.inbound.order;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.business.outbound.entity.WmsOutReceiptTask;
import org.jeecg.modules.business.outbound.service.IWmsOutReceiptTaskService;
import org.jeecg.modules.business.third.service.IMesStatusService;
import org.jeecg.modules.business.transfer.entity.WmsMoveReceipt;
import org.jeecg.modules.business.transfer.entity.WmsMoveReceiptMaterial;
import org.jeecg.modules.business.transfer.service.IWmsMoveReceiptMaterialService;
import org.jeecg.modules.business.transfer.service.IWmsMoveReceiptService;
import org.jeecg.modules.business.warehouse.entity.WmsInReceiptTask;
import org.jeecg.modules.business.warehouse.service.IWmsInReceiptTaskService;
import org.jeecg.modules.dock.config.JsonUtils;
import org.jeecg.modules.dock.entity.mes.request.MesBaseRequestVO;
import org.jeecg.modules.dock.entity.mes.response.MesBaseResponseVO;
import org.jeecg.modules.dock.service.MesDockingRepeaterService;
import org.jeecg.modules.flow.sys.common.BaseCustomBeanRun;
import org.jeecg.modules.flow.sys.entity.dto.BaseFlowDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * 通知MES任务开始
 *
 * @author FanZhiwen
 * @version 1.0.0
 * @since 2023-11-20
 */
@Component
@Slf4j
public class SendMesStartInitBean<T> extends BaseCustomBeanRun<T> {
    @Autowired
    private IWmsInReceiptTaskService iWmsInReceiptTaskService;
    @Autowired
    private IWmsOutReceiptTaskService wmsOutReceiptTaskService;
    @Autowired
    private IMesStatusService iMesStatusService;
    @Autowired
    private IWmsMoveReceiptService iWmsMoveReceiptService;
    @Autowired
    private IWmsMoveReceiptMaterialService IWmsMoveReceiptMaterialService;
    @Autowired
    private MesDockingRepeaterService mesDockingRepeaterService;

    @Override
    public void actureRun() {
        BaseFlowDto baseFlowDto = this.getBaseFlowDto();
        String taskId = baseFlowDto.getInfoId();
        WmsInReceiptTask inTask = iWmsInReceiptTaskService.getById(taskId);
        WmsOutReceiptTask outTask = wmsOutReceiptTaskService.getById(taskId);
        WmsMoveReceipt moveReceipt = iWmsMoveReceiptService.getById(baseFlowDto.getInfoId());
        boolean result = false;
        try {
            if (ObjectUtil.isNotNull(inTask)){
                result = iMesStatusService.warehouse(inTask.getId(),null,1,inTask);
            }
            if (ObjectUtil.isNotNull(outTask)){
                result = iMesStatusService.outbound(outTask.getId(),null,1,outTask);
            }
            if (ObjectUtil.isNotNull(moveReceipt)){
                WmsMoveReceiptMaterial wmsMoveReceiptMaterial = IWmsMoveReceiptMaterialService.getOne(new LambdaQueryWrapper<WmsMoveReceiptMaterial>()
                        .eq(WmsMoveReceiptMaterial::getWmsMoveReceiptId, moveReceipt.getId()).last("limit 1"));
                if (ObjectUtil.isNotNull(wmsMoveReceiptMaterial)) {
                    if (StringUtils.isNotBlank(wmsMoveReceiptMaterial.getExtraText())) {
                        MesBaseRequestVO bean = JsonUtils.toBean(wmsMoveReceiptMaterial.getExtraText(), MesBaseRequestVO.class);
                        bean.setTaskStatus("TASK_STARTED");
                        bean.setOperateTime(new Date());
                        // 通知MES消息
                        MesBaseResponseVO mesBaseResponseVO = mesDockingRepeaterService.updateStatus(bean);
                        if ("success".equals(mesBaseResponseVO.getTarget())) {
                            result = true;
                        }
                    }
                }
            }
        }catch (Exception e){
            log.error("通知MES出现异常",e);
            baseFlowDto.setNextFailed(true);
            baseFlowDto.setResult(Result.error("通知MES执行失败: "+e.getMessage()));
        }
        if (!result){
            baseFlowDto.setNextFailed(true);
            baseFlowDto.setResult(Result.error("通知MES执行失败"));
        }
    }
}
